import {
  queryStationList,
  queryWaterUse,
  queryHdStationList,
  queryScadeYLdata,
} from '../services/index';

export default {
  namespace: 'index',

  state: {
    SupplyWaterResource: {},
    WaterUseResource: [],
  },

  effects: {
    *queryStationList({ payload: params }, { call, put }) {
      const { type, ...payload } = params;
      const data = yield call(queryStationList, payload)
      const { success, message, status, ...other } = data
      if (success) {
        yield put({
          type: 'setState',
          payload: {
            [`SupplyWaterResource${type}`]: other.Response,
          },
        })
      } else {
        throw data
      }
    },
    *queryWaterUse({ payload: params }, { call, put }) {
      const { type, ...payload } = params;
      const data = yield call(queryWaterUse, payload)
      const { success, message, status, ...other } = data
      if (success) {
        yield put({
          type: 'setState',
          payload: {
            [`WaterUseResource${type}`]: other.data,
          },
        })
      } else {
        throw data
      }
    },
    *queryHdStationList({ payload: params }, { call, put }) {
      const { type, ...payload } = params;
      // 解构 type，返回结果拼接
      const data = yield call(queryHdStationList, payload)
      const { success, message, status, ...other } = data
      if (success) {
        yield put({
          type: 'setState',
          payload: {
            [`HdStationListResource${type}`]: other.Response.Data[0],
          },
        })
      } else {
        throw data
      }
    },
    *queryScadeYLdata({ payload }, { call, put }) {
      const data = yield call(queryScadeYLdata, payload)
      const { success, message, status, ...other } = data
      if (success) {
        yield put({
          type: 'setState',
          payload: {
            YLResource: other.Response[0],
          },
        })
      } else {
        throw data
      }
    },
  },

  reducers: {
    setState(state, action) {
      return {
        ...state,
        ...action.payload
      };
    }
  },
}
